
class Solution {
    public int maximumUniqueSubarray(int[] nums) {
        Map<Integer, Integer> cnt = new HashMap<>();
        int ans = 0;
        int tmp = 0;
        int left = 0;
        for (int i = 0; i < nums.length; i++) {
            if (cnt.containsKey(nums[i])) {
                int n = cnt.get(nums[i]);
                for (int j = left; j <= n; j++) {
                    tmp -= nums[j];
                    cnt.remove(nums[j]);
                }
                left = n + 1;
            }
            cnt.put(nums[i], i);
            tmp += nums[i];
            ans = Math.max(ans, tmp);
        }
        return ans;
    }
}
